/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package orderProcessingProcess.server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.ws.Endpoint;

import orderProcessingProcess.callbackHandlerSecurity.ServerPasswordCallbackHandler;
import orderProcessingProcess.dataStorage.TestDataEntry;
//import org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.apache.cxf.jaxrs.JAXRSBindingFactory;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;

/**
 * 
 * @author dreni
 */
public class Server {

	private static final String SMSSERVICE = "http://localhost:8088/smsservice";
	private static final String MAILSERVICE = "http://localhost:8089/mailservice";
	private static final String ORDERSERVICE = "http://localhost:8083/orderservice";
	private static final String BANKINGSERVICE = "http://localhost:8084/bankingservice";
	private static final String CUSTOMERSERVICE = "http://localhost:8085/customers";
	private static final String CUSTOMERSERVICESOAP = "http://localhost:8086/customersoap";

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) throws Exception {
		
		//boolean security = true;
		boolean security = false;  //we need the nonsecure mode for ths second assignment
		//check wich kind of service to start: secure or nonsecure
		if (args.length > 0) {
    		if (args[0].equals("nonsecure")) {
    			security = false;
    			System.out.println("SERVER CLASS ARGUMENT IS: "+args[0].toString() );
    		}
		}
		
		System.out.println("Starting Server...");

		// publish the services

		/*******************************************
		 * Publish SMSSERVICE
		 */
		SMSServiceImpl smsServiceImpl = new SMSServiceImpl();
		Endpoint.publish(SMSSERVICE, smsServiceImpl);
		// @SuppressWarnings("unused")
		// EndpointImpl jaxWsEndpointSMS = (org.apache.cxf.jaxws.EndpointImpl)
		// Endpoint.publish(SMSSERVICE, smsServiceImpl);

		System.out.println("SMSService published >>>>>>> " + SMSSERVICE);

		/*******************************************
		 * Publish MAILSERVICE
		 */
		NotifyService mailServiceImpl = new MailServiceImpl();
		Endpoint.publish(MAILSERVICE, mailServiceImpl);
		System.out.println("MailService published >>>>>>> " + MAILSERVICE);

		/*******************************************
		 * Publish ORDERSERVICE
		 */
		OrderManagementService orderServiceImpl = new OrderManagementServiceImpl();
		Endpoint.publish(ORDERSERVICE, orderServiceImpl);
		System.out.println("OrderService published >>>>>>> " + ORDERSERVICE);

		
		/*******************************************
		 * Publish BANKINGSERVICE
		 */
		BankingService bankingServiceImpl = new BankingServiceImpl();
		EndpointImpl jaxWsEndpointBANK = (org.apache.cxf.jaxws.EndpointImpl) Endpoint
				.publish(BANKINGSERVICE, bankingServiceImpl);
		
		if(security){
			// get CXFEndpoint of the BankService
			org.apache.cxf.endpoint.Endpoint cxfEndpointBANK = jaxWsEndpointBANK
					.getServer().getEndpoint();
			
			// Now add the interceptors for Password
			Map<String, Object> inPropsBank = new HashMap<String, Object>();
			inPropsBank.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
			inPropsBank.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
			inPropsBank.put(WSHandlerConstants.PW_CALLBACK_CLASS, ServerPasswordCallbackHandler.class.getName());

			//cxfEndpointBANK.getInInterceptors().add(new SAAJInInterceptor()); // 2.0.x only; not needed in 2.1+
			WSS4JInInterceptor wssInNp = new WSS4JInInterceptor(inPropsBank);
			cxfEndpointBANK.getInInterceptors().add(wssInNp);
			System.out.println("[debug] SECURE BankingService published >>>>>>> " + BANKINGSERVICE);
		}
		
		System.out.println("BankingService published >>>>>>> " + BANKINGSERVICE);

		/*******************************************
		 * Start CUSTOMERSERVICE (REST)
		 */

		JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
		sf.setResourceClasses(CustomerManagementServiceImpl.class);
		sf.setBindingId(JAXRSBindingFactory.JAXRS_BINDING_ID);        
		sf.setAddress(CUSTOMERSERVICE);
		sf.create();
        	
		System.out.println("CustomerManagementService (REST) started  >>>>>>> " + CUSTOMERSERVICE);
		
		System.out.println("Entrying test data... Please Wait!");
		new TestDataEntry();

		/*******************************************
		 * Publish CUSTOMERSERVICE (SOAP)
		 */
		CustomerManagementServiceSOAP customerServiceSOAPImpl = new CustomerManagementServiceSOAPImpl();
		Endpoint.publish(CUSTOMERSERVICESOAP, customerServiceSOAPImpl);
		System.out.println("CustomerManagementService (SOAP) published >>>>>>> " + CUSTOMERSERVICESOAP);


		System.out.println("Press ENTER to stop the server!");

		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		stdin.readLine();

		System.out.println("Server exiting");
		System.exit(0);
	}
}
